<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <base href="#(basePath)"/>
  <title>交易管理 - 充值制单</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="static/layui/css/layui.css" media="all">
</head>
<body>
  <div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 20px 0 0;">
  	  <input type="hidden" name="tradeOrder.youliAccountId">
	  <div class="layui-form-item">
	    <div class="layui-form-item">
		    <label class="layui-form-label">用户编号</label>
		    <div class="layui-input-block">
	      		<input type="text" name="tradeOrder.merchCode" required  lay-verify="required" placeholder="请输入商户编号或名称" autocomplete="off" class="layui-input">
	    		</div>
	    </div>
	    <div class="layui-form-item">
		    <label class="layui-form-label">用户名称</label>
		    <div class="layui-input-block">
	      		<input type="text" name="tradeOrder.merchName" required  lay-verify="required" readonly="readonly" autocomplete="off" class="layui-input">
	    		</div>
	    </div>

		<div class="layui-form-item">
		    <label class="layui-form-label">账户别名</label>
		    <div class="layui-input-block">
	      		<input type="text" name="alias" required  lay-verify="required" placeholder="请输入账户别名或账户名" autocomplete="off" class="layui-input">
	    		</div>
	    </div>

	    <div class="layui-form-item">
		    <label class="layui-form-label">账户名</label>
		    <div class="layui-input-block">
	      		<input type="text" name="accountName" required  lay-verify="required" readonly="readonly" autocomplete="off" class="layui-input">
	    		</div>
	    </div>

	    <div class="layui-form-item">
		    <label class="layui-form-label">账号</label>
		    <div class="layui-input-block">
	      		<input type="text" name="accountNo" required  lay-verify="required" readonly="readonly" autocomplete="off" class="layui-input">
	    		</div>
	    </div>

	  </div>

	  <div class="layui-form-item">
	    <label class="layui-form-label">金额</label>
	   <div class="layui-input-block">
	      <input type="text" id="chargeAmount" name="amount" required placeholder="金额" oninput="if(value.length>10)value=value.slice(0,10)" class="layui-input">
	    </div>
	  </div>

	  <div class="layui-form-item">
	    <label class="layui-form-label">金额大写</label>
	    <div class="layui-input-block">
	    		 <input type="text" id="chargeAmountShowBig" readonly="readonly" class="layui-input">
	    </div>
	  </div>

	  <div class="layui-form-item">
	    <label class="layui-form-label">买家名称</label>
	   <div class="layui-input-block">
	      <input type="text" name="tradeOrder.payer" placeholder="买家名称"  required  lay-verify="required"  autocomplete="off" class="layui-input">
	    </div>
	  </div>
	  <div class="layui-form-item">
	    <label class="layui-form-label">买家电话</label>
	   <div class="layui-input-block">
	      <input type="text" name="tradeOrder.phoneNo" placeholder="买家电话" maxlength="11" autocomplete="off" class="layui-input">
	    </div>
	  </div>
	  <div class="layui-form-item">
	    <label class="layui-form-label">商品描述</label>
	   <div class="layui-input-block">
	      <input type="text" name="tradeOrder.goodsName" placeholder="商品描述" autocomplete="off" class="layui-input">
	    </div>
	  </div>
	  <div class="layui-form-item layui-hide">
	    <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" id="layuiadmin-app-form-submit" value="确认">
	  </div>
  </div>

  <script src="static/layui/layui.js"></script>
  <script src="static/menoy.js"></script>
  <script type="text/javascript">
  function convertCurrency(money) {
		//汉字的数字
		var cnNums = new Array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
		//基本单位
		var cnIntRadice = new Array('', '拾', '佰', '仟');
		//对应整数部分扩展单位
		var cnIntUnits = new Array('', '万', '亿', '兆');
		//对应小数部分单位
		var cnDecUnits = new Array('角', '分', '毫', '厘');
		//整数金额时后面跟的字符
		var cnInteger = '整';
		//整型完以后的单位
		var cnIntLast = '元';
		//最大处理的数字
		var maxNum = 999999999999999.9999;
		//金额整数部分
		var integerNum;
		//金额小数部分
		var decimalNum;
		//输出的中文金额字符串
		var chineseStr = '';
		//分离金额后用的数组，预定义
		var parts;
		if (money == '') { return ''; }
		money = parseFloat(money);
		//超出最大处理数字
		if (money >= maxNum) { return ''; }
		if (money == 0) {
			chineseStr = cnNums[0] + cnIntLast + cnInteger;
			return chineseStr;
		}
		//转换为字符串
		money = money.toString();
		if (money.indexOf('.') == -1) {
			integerNum = money;
			decimalNum = '';
		} else {
			parts = money.split('.');
			integerNum = parts[0];
			decimalNum = parts[1].substr(0, 4);
		}
		//获取整型部分转换
		if (parseInt(integerNum, 10) > 0) {
			var zeroCount = 0;
			var IntLen = integerNum.length;
			for (var i = 0; i < IntLen; i++) {
				var n = integerNum.substr(i, 1);
				var p = IntLen - i - 1;
				var q = p / 4;
				var m = p % 4;
				if (n == '0') {
					zeroCount++;
				} else {
					if (zeroCount > 0) {
						chineseStr += cnNums[0];
					}
					//归零
					zeroCount = 0;
					chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
				}
				if (m == 0 && zeroCount < 4) {
					chineseStr += cnIntUnits[q];
				}
			}
			chineseStr += cnIntLast;
		}
		//小数部分
		if (decimalNum != '') {
			var decLen = decimalNum.length;
			for (var i = 0; i < decLen; i++) {
				var n = decimalNum.substr(i, 1);
				if (n != '0') {
					chineseStr += cnNums[Number(n)] + cnDecUnits[i];
				}
			}
		}
		if (chineseStr == '') {
			chineseStr += cnNums[0] + cnIntLast + cnInteger;
		} else if (decimalNum == '') {
			chineseStr += cnInteger;
		}
		return chineseStr;
	}
  </script>
  <script>
  layui.config({
	base: '#(basePath)' //静态资源所在路径
  }).extend({
    index: 'lib/index',
  }).use(['index', 'form','autocomplete'], function(){
    var $ = layui.$
    ,form = layui.form
    ,autocomplete = layui.autocomplete;

    //自动填充商户号和商户名
    autocomplete.render({
            elem: $('input[name="tradeOrder.merchCode"]')[0],
            url: layui.setter.base+"admin/merch/autocomplete",
            template_val: '{{d.code}}',
            template_txt: '{{d.code}} <span class=\'layui-badge layui-bg-gray\'>{{d.name}}</span>',
            onselect: function (resp) {
            		$('input[name="tradeOrder.merchCode"]').val(resp.code),
            		$('input[name="tradeOrder.merchName"]').val(resp.name)
            }
        }),

    //自动填充账户信息
    autocomplete.render({
            elem: $('input[name="alias"]')[0],
            url: layui.setter.base+"admin/youliAccount/autocomplete",
            template_val: '{{d.alias}}',
            template_txt: '{{d.alias}} <span class=\'layui-badge layui-bg-gray\'>{{d.accountName}}-{{d.accountNo}}</span>',
            onselect: function (resp) {
            		$('input[name="alias"]').val(resp.alias),
            		$('input[name="accountName"]').val(resp.accountName),
            		$('input[name="accountNo"]').val(resp.accountNo),
            		$('input[name="tradeOrder.youliAccountId"]').val(resp.id)
            }
        }),

    $("#chargeAmount").bind("input",function(event){
    		var money = this.value;
    		var big = convertCurrency(money)
    		$("#chargeAmountShowBig").val(big);
    	});

    //监听提交
    form.on('submit(layuiadmin-app-form-submit)', function(data){
      var field = data.field; //获取提交的字段
      var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
      //提交 Ajax 成功后，关闭当前弹层并重载表格
      $.ajax({
    	  url: layui.setter.base+"admin/charge/add",
    	  type:"post",
    	  data: field,
    	  success: function(res){
			  if(res.code !== 0){
				parent.layer.msg(res.msg, {icon: 2});
			  }else{
	    		  parent.layer.msg("添加成功", {icon: 1});
	    	      parent.layui.$("#LAY_app_body > div.layui-show iframe").get()[0].contentWindow.layui.table.reload('LAY-web-list'); //重载表格
	    	      parent.layer.close(index); //再执行关闭
			  }
    	  },
    	  error: function(xhr){parent.layer.msg("出错了" + xhr);}
      });
    });
  })


  </script>

</body>
</html>
